﻿using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;

namespace pinduoduo.Util
{
    public class SystemUtil
    {

        [StructLayout(LayoutKind.Sequential)]
        struct LASTINPUTINFO
        {
            // 设置结构体块容量
            [MarshalAs(UnmanagedType.U4)]
            public int cbSize;
            // 捕获的时间
            [MarshalAs(UnmanagedType.U4)]
            public uint dwTime;
        }

        [DllImport("user32.dll")]
        private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
   
        private static void IdleTime(object sender, EventArgs e)
        {
         
        }

        /// <summary>
        /// 将字符串转成二进制
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string bianma(string s)
        {
            byte[] data = Encoding.Unicode.GetBytes(s);
            StringBuilder result = new StringBuilder(data.Length * 8);

            foreach (byte b in data)
            {
                result.Append(Convert.ToString(b, 2).PadLeft(8, '0'));
            }
            return result.ToString();
        }

        /// <summary>
        /// 将二进制转成字符串
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string jiema(string s)
        {
            System.Text.RegularExpressions.CaptureCollection cs =
                System.Text.RegularExpressions.Regex.Match(s, @"([01]{8})+").Groups[1].Captures;
            byte[] data = new byte[cs.Count];
            for (int i = 0; i < cs.Count; i++)
            {
                data[i] = Convert.ToByte(cs[i].Value, 2);
            }
            return Encoding.Unicode.GetString(data, 0, data.Length);
        }

        /// <summary>
        /// 系统是否被锁定
        /// </summary>
        /// <param name="service"></param>
        /// <param name="tu"></param>
        /// <returns>true表示被锁定</returns>
        public static bool IsLocking(out string msg)
        {
            if (File.Exists(@"mm.temp"))
            {
                var temp = File.ReadLines(@"mm.temp");
                foreach (var item in temp)
                {
                    DateTime dt1 = Convert.ToDateTime(jiema(item));
                    DateTime dt2 = DateTime.Now;
                    TimeSpan ts1 = new TimeSpan(dt1.Ticks);
                    TimeSpan ts2 = new TimeSpan(dt2.Ticks);
                    TimeSpan ts = ts1- ts2;
                    if (DateTime.Compare(dt1, dt2) > 0)
                    {
                        string dateDiff = Math.Ceiling(ts.TotalMinutes).ToString();
                        msg = $"系统已锁定，请{dateDiff}分钟后登录";
                        return true;
                    }
                    break;
                }

            }
            msg = "";
            return false;
        }

        /// <summary>
        /// 系统锁定
        /// </summary>
        public static void LockSystem()
        {
            DateTime dateTime = DateTime.Now.AddMinutes(5);
            File.WriteAllText(@"mm.temp", SystemUtil.bianma(dateTime.ToString()));
        }


        /// <summary>
        /// 系统锁定
        /// </summary>
        public static void RememberLoginUser(string userName,string password)
        {
            File.WriteAllText(@"ml.temp", userName+"+=+"+password);
        }

        /// <summary>
        /// 系统锁定
        /// </summary>
        public static void ReadLoginUser(ref string userName, ref string password)
        {
            if (File.Exists(@"ml.temp"))
            {
                var temp = File.ReadAllText(@"ml.temp");
                var arr = temp.Split(new []{ "+=+" }, StringSplitOptions.None);
                if (arr.Length == 2) {
                    userName = arr[0];
                    password = arr[1];
                }
            }
        }

        /// <summary>
        /// 获取时间戳
        /// </summary>
        /// <returns></returns>
        public static double GetTimeStamp()
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return ts.TotalMilliseconds;
        }

        public static long DateTimeToTicks(DateTime time)
        {
            long value = time.ToUniversalTime().Ticks;
            return value;
        }

        public static DateTime DateTime1970 = new DateTime(1970, 1, 1).ToLocalTime();

        /// <summary>
        /// 计算 1970-01-01 到指定 <see cref="DateTime"/> 的毫秒数。
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static double GetTimeStamp(DateTime dateTime)
        {
            return (dateTime.ToUniversalTime() - DateTime1970).TotalMilliseconds;
        }
    }
}